package cn.lj.logistics.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.tags.EditorAwareTag;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.lj.logistics.pojo.MsgObject;
import cn.lj.logistics.pojo.BaseData;
import cn.lj.logistics.pojo.BaseDataExample;
import cn.lj.logistics.pojo.BaseData;
import cn.lj.logistics.pojo.BaseDataExample;
import cn.lj.logistics.pojo.BaseDataExample.Criteria;
import cn.lj.logistics.pojo.User;
import cn.lj.logistics.pojo.UserExample;
import cn.lj.logistics.service.BaseDataService;
import cn.lj.logistics.service.UserService;
import cn.lj.logistics.service.BaseDataService;
import net.sf.jsqlparser.statement.delete.Delete;

@Controller
@RequestMapping("/baseData")
public class BaseDataController {
     
	@Autowired
	private BaseDataService baseDataService;
	
	@Autowired UserService userService;
	
	//编辑页面，新增和修改公用的页面
	@RequestMapping("/edit")
	public String edit(Model m,Long baseId) {
		
		//查询出所有基础数据中 parent_id = null 类别数据
		
		BaseDataExample example=new BaseDataExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdIsNull();
		List<BaseData> parents = baseDataService.selectByExample(example);
		m.addAttribute("parents", parents);
		//修改操作
		if (baseId!=null) {
			// 查询出当前用户的数据，共享给编辑页面，以供修改回显数据
			BaseData baseData = baseDataService.selectByPrimaryKey(baseId);
			m.addAttribute("baseData",baseData);
		}
	
		return "baseDataEdit";
		
	}
	

	
	//新增功能
		@RequestMapping("/insert")
		@ResponseBody
		public MsgObject insert(BaseData baseData) {
			
			
		
		int row = baseDataService.insert(baseData);
			MsgObject mo = MsgObject.createMO(0, "新增数据失败，请联系管理员！！！");
			if (row==1) {
				mo=MsgObject.createMO(1, "新增数据成功");
			}
			return mo;
			
		}
		
		//修改功能
				@RequestMapping("/update")
				@ResponseBody
				public MsgObject update(BaseData baseData) {
				
				
				int row = baseDataService.updateByPrimaryKeySelective(baseData);
					MsgObject mo = MsgObject.createMO(0, "修改数据失败，请联系管理员！！！");
					if (row==1) {
						mo=MsgObject.createMO(1, "修改数据成功");
					}
					return mo;
					
				}
		
	
	//删除功能
	@RequestMapping("/delete")
	@ResponseBody
	public MsgObject delete(Long baseId) {
		
		BaseDataExample example=new BaseDataExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(baseId);
		List<BaseData> children = baseDataService.selectByExample(example);
	
		MsgObject mo = MsgObject.createMO(0, "删除数据失败，请联系管理员！！");
		
		if (children.size()>0) {
			 mo = MsgObject.createMO(0, "此数据还有子数据，不能删除！！");
		   return mo;
		}
		
		int row = baseDataService.deleteByPrimaryKey(baseId);
	
		if (row==1) {
			mo=MsgObject.createMO(1, "删除数据成功");
		}
		return mo;
		
	}
	

	
	@RequestMapping("/baseDataPage")
	@RequiresPermissions("baseData:baseDataPage")
	public String baseDataPage() {
		
		return "baseDataPage";
		
	}
	

	@RequestMapping("/list")
	@RequiresPermissions("baseData:list")
	@ResponseBody
	public PageInfo<BaseData> list(@RequestParam(defaultValue = "1")Integer pageNum,
			               @RequestParam(defaultValue = "10")Integer pageSize ,String keyword){
		System.out.println("keyword:"+ keyword);
		//开始分页
		PageHelper.startPage(pageNum, pageSize);
		BaseDataExample example=new BaseDataExample();
		
		if (StringUtils.isNotBlank(keyword)) {
			// 创建限制对象
			Criteria criteria1 = example.createCriteria();
		criteria1.andBaseNameLike("%"+keyword+"%"); 

	
		}
		List<BaseData> list = baseDataService.selectByExample(example);
		
		return  new PageInfo<BaseData>(list);
		
	}
}
